/***************************************************************************
 *    
 *      Copyright (c) 2009,2010 KaiTrade LLC (registered in Delaware)
 *                     All Rights Reserved Worldwide
 *
 * STRICTLY PROPRIETARY and CONFIDENTIAL
 *
 * WARNING:  This file is the confidential property of KaiTrade LLC For
 * use only by those with the express written permission and license from
 * KaiTrade LLC.  Unauthorized reproduction, distribution, use or disclosure 
 * of this file or any program (or document) is prohibited. 
 * 
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace K2AlgoSet
{
    /// <summary>
    /// This provides a simple base class for all expressions.
    /// </summary>
    public class ExpressionBase : KaiTrade.Interfaces.StatExpression
    {
        protected KaiTrade.Interfaces.TSSet m_TSSet = null;
        protected string m_Expression = "";
        protected log4net.ILog m_Log;
        protected string m_Name = "";

        public ExpressionBase()
        {
            m_Log = log4net.LogManager.GetLogger("KaiAlgo");
        }

        #region StatExpression Members

        public virtual string Name
        {
            get { return m_Name; }
        }

        public virtual KaiTrade.Interfaces.TSSet TSSet
        {
            get
            {
                return m_TSSet;
            }
            set
            {
                m_TSSet = value;
            }
        }

        public virtual double Value
        {
            get{return 0;} 
        }

        /// <summary>
        /// the text of the expression - will be parsed for parameters
        /// </summary>
        public virtual string Expression
        {
            get { return m_Expression; }
            set
            {
                m_Expression = value;
                parseExpression(m_Expression);
            }
        }

        protected virtual void parseExpression(string myExpression)
        {
        }


        /// <summary>
        /// Evaluate the expression on the vurrent data, normally should use
        /// the last complete item/bar - setting bUseCurrent bar will cause
        /// the evaluation to include the bar being added to, that has yet to
        /// end.
        /// </summary>
        public virtual void EvaluateAdded(bool bUseCurrentBar)
        {
           
        }

        public virtual void EvaluateUpdate(bool bUseCurrentBar)
        {
            
        }

        /// <summary>
        /// Return the XML rendering of the expression
        /// </summary>
        /// <returns></returns>
        public KAI.kaitns.StatExpression ToXMLDB()
        {
            KAI.kaitns.StatExpression myRet = new KAI.kaitns.StatExpression();
            try
            {
                myRet.Expression = m_Expression;
                string[] myExpParms = m_Expression.Split(',');
                myRet.Name = myExpParms[0];
            }
            catch (Exception myE)
            {
                m_Log.Error("ToXMLDB", myE);
            }
            return myRet;
        }

        /// <summary>
        /// Render the expression in XML
        /// </summary>
        /// <param name="myMatcher"></param>
        public void FromXMLDB(KAI.kaitns.StatExpression myExpression)
        {
            try
            {
                this.Expression = myExpression.Expression;
                m_Name = myExpression.Name; 
            }
            catch (Exception myE)
            {
                m_Log.Error("ToXMLDB", myE);
            }
        }

        #endregion
    }
}
